from utils.base_models import BaseModel
from django.db import models
from apps.HttpTestcase.core.utils import get_version as version


class TestsuiteTestcaseReports(BaseModel):
    '''
    场景报告下用例信息
    '''
    testcase_id = models.CharField(null=False, blank=False, max_length=128, verbose_name='用例ID')
    testcase_name = models.CharField(null=False, blank=False, max_length=128, verbose_name='用例名称')
    api = models.ForeignKey(to='Interfaces', related_name='api', on_delete=models.SET_NULL, null=True, blank=True,
                            verbose_name='所属接口')
    api_name = models.CharField(null=False, blank=False, max_length=128, verbose_name='接口名称')
    actual_status_code = models.CharField(max_length=11, null=False, blank=False, verbose_name='实际响应状态码')
    elapsed_ms = models.DecimalField(max_digits=10, decimal_places=3, null=False, blank=False, verbose_name='响应时长(ms)')
    actual_response_data = models.TextField(null=False, blank=False, verbose_name='实际响应结果')
    failure_reason = models.TextField(null=True, blank=True, verbose_name='测试未通过原因')
    version = models.CharField(max_length=13, null=False, blank=False, default=version, verbose_name='版本号')
    url = models.TextField(verbose_name='url', null=True, blank=True)
    status_choice = (
        ('PASS', '通过'),
        ('FAIL', '失败')
    )
    status = models.CharField(max_length=11, null=False, blank=False, default='FAIL', choices=status_choice,
                              verbose_name='是否测试通过')
    testsuite_reports = models.ForeignKey(to='TestsuiteReports', related_name='testsuite_report',
                                          on_delete=models.CASCADE, null=False, blank=False, verbose_name='绑定场景报告ID')
    execute_time = models.DateTimeField(verbose_name='执行时间')
    project_name = models.CharField(null=False, blank=False, max_length=128, verbose_name='项目名称')
    # method_choice = (
    #     ('GET', 'GET'),
    #     ('PUT', 'PUT'),
    #     ('POST', 'POST'),
    #     ('DELETE', 'DELETE')
    # )
    # method = models.CharField(max_length=11, null=False, blank=False, default='GET', choices=method_choice,
    #                           verbose_name='接口请求方法')
    # headers = models.TextField(null=True, blank=True, verbose_name='接口请求头信息')
    # request_data_type_choice = (
    #     ('Params', 'Params'),
    #     ('Json', 'Json'),
    #     ('Form Data', 'Form Data')
    # )
    # request_data_type = models.CharField(max_length=11, null=False, blank=False, default='Json',
    #                                      choices=request_data_type_choice, verbose_name='接口请求参数类型')
    # request_data = models.TextField(null=True, blank=True, verbose_name='接口请求参数')
        # project = models.ForeignKey(to='Projects', related_name='+', on_delete=models.SET_NULL, null=True, blank=True,
    #                             verbose_name='所属项目')

    # testcase = models.ForeignKey(to='Testcases', related_name='testcase_result', on_delete=models.SET_NULL, null=True,
    #                              blank=True, verbose_name='所属用例')
    # is_periodictask = models.BooleanField(null=False, blank=False, default=True, verbose_name='是否是定时任务')

    class Meta:
        db_table = 'http_testsuite_testcase_reports'
        verbose_name = '场景下用例结果'
        verbose_name_plural = verbose_name
        ordering = ('-create_time', )

    def __str__(self):
        return self.testcase_name
